﻿using HandlerLibrary.Comm;
using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers
{
    public class HomeController : Controller
    {
        ZHHQSERVICE.Service1Client service = new ZHHQSERVICE.Service1Client();

        public ActionResult Index()
        {            
            try
            {
                #region 门户登陆
                   var LoginUser = Request.QueryString["LOGINUSER"];
                   Log.Debug("加密门户登陆：",LoginUser);
                   if (LoginUser != null)
                   {
                       //解密
                       string un = JM.Decode(LoginUser);
                       string[] op = un.Split('|');
                       Model.View_OperUser vop = new Model.View_OperUser();
                       string res = service.OperUserLoginNoPwd(op[0], "", 1);
                       JObject searcho = JObject.Parse(res);//将服务返回的结果进行优化
                       string bs = searcho["BS"].ToString();
                       if (bs != "1")
                       {
                           //跳转到登录页
                           return RedirectToAction("LoginPage", "Login");

                       }
                       IList<JToken> jieguo = searcho["rows"].Children().ToList();//将json数据转成多条
                       vop = obser<DataResult>.DataContractJsonDeserialize<Model.View_OperUser>(searcho["rows"].ToString());//将json格式转为实体类
                       Session[SessionStorage.LogMethod] = 2;
                       Log.Debug("LOGINUSER登陆保存Session", vop.OperID.ToString());
                       Session[SessionStorage.OperUser] = vop;//将实体类保存至Session
                       Session[SessionStorage.UserLogin] = null;
                       new ZBZH_Manage_Order.PublicMethod.AppDictionary();
                   }
                #endregion

                string[] PowerS = null;
                if ((int)Session[SessionStorage.LogMethod] == 1)
                {
                    Model.View_User user = (Model.View_User)Session[SessionStorage.UserLogin];
                    PowerS = user.Login_AccRole_Role_power.Split(',');//权限码
                }
                else
                {                 
                    Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                    PowerS = user.RolePower.Split(',');//权限码
                }

                List<CDList> lsk = new List<HomeController.CDList>();//菜单list
                List<string> DCDls = (from u in PowerS where u.Length == 3 select u).ToList();//主菜单的id
                foreach (var item in DCDls)
                {
                    CDList Dll = new CDList();
                    Dll.name = Method_返回主菜单名字(item);
                    if (Dll.name != "")
                    {
                        List<string> ZCDstrls = (from u in PowerS where u.Length == 6 & u.StartsWith(item) select u).ToList();
                        List<Zcd> ZCDls = new List<Zcd>();
                        foreach (var zitem in ZCDstrls)
                        {
                            ZCDls.Add(Method_返回子菜单(zitem));
                        }

                        Dll.zcdlist = ZCDls;
                        lsk.Add(Dll);
                    }
                }
                ViewBag.ll = lsk;
              
             
                return View();            

            }
            catch (Exception ex) 
            {
                Log.Debug("异常：", ex.ToString()); 
                return RedirectToAction("LoginPage", "Login");  //czx，修改为登录者的Session异常时，直接跳转到登录页面
            }
        }
        string Method_返回主菜单名字(string id)
        {
            string ret = "";
            switch (id)
            {
                case "008":
                    ret = "系统管理";
                    break;
                case "009":
                    ret = "商户管理";
                    break;
                case "010":
                    ret = "用户管理";
                    break;
                case "011":
                    ret = "流水管理";
                    break;
                case "015":
                    ret = "报表管理";
                    break;
                case "012":
                    ret = "派送管理";
                    break;
                default:
                    break;
            }
            return ret;
        }
        Zcd Method_返回子菜单(string id)
        {
            Zcd m = new Zcd();
            switch (id)
            {
                case "008001":
                    m.id = "/SystemSet/Index";
                    m.name = "系统设置";
                    break;
                case "008002":
                    m.id = "/MerDefinition/Index";
                    m.name = "商户定义";
                    break;
                case "008003":
                    m.id = "/MerOperSet/Index";
                    m.name = "商户操作员设置";
                    break;
                case "008004":
                    m.id = "/WaltSet/Index";
                    m.name = "钱包设置";
                    break;
                case "008005":
                    m.id = "/ThirdPartyPayment/Index";
                    m.name = "第三方支付设置";
                    break;
                case "008006":
                    m.id = "/IntegralManage/Index";
                    m.name = "平台积分管理";
                    break;
                case "008007":
                    m.id = "/SystemLog/Index";
                    m.name = "系统日志";
                    break;
                case "008008":
                    m.id = "/OrganizationSet/Index";
                    m.name = "企业帐号管理";
                    break;
                case "008009":
                    m.id = "/OrganizationRepayment/Index";
                    m.name = "企业帐号平帐";
                    break;
                case "008010":
                    m.id = "/AddressManagement/Index";
                    m.name = "送餐地址管理";
                    break;
                case "008011":
                    m.id = "/Order_Information/Index";
                    m.name = "商城信息管理";
                    break;
                case "009001":
                    m.id = "/MerSet/Index";
                    m.name = "商户设置";
                    break;
                case "009002":
                    m.id = "/MerchantsIntegralManage/Index";
                    m.name = "商户积分管理";
                    break;
                case "009003":
                    m.id = "/MerAccount/Index";
                    m.name = "商户账目";
                    break;
                case "009005":
                    m.id = "/Order_Dishes/Index";
                    m.name = "商品管理";
                    break;
                case "009004":
                    m.id = "/Order_DishesType/Index";
                    m.name = "商品分类设置";
                    break;
                case "009006":
                    m.id = "/Order_Shelves/Index";
                    m.name = "上架管理";
                    break;
                case "009007":
                    m.id = "/Order_Form/Index";
                    m.name = "订单管理";
                    break;
                case "009008":
                    m.id = "/Order_Eva/Index";
                    m.name = "评价管理";
                    break;
                case "009009":
                    m.id = "/Order_MerPricing/Index";
                    m.name = "订单优惠";
                    break;
                case "009010":
                    m.id = "/Sales/Index";
                    m.name = "销售管理";
                    break;
                case "009011":
                    m.id = "/WxMsg/Index";
                    m.name = "微信通知管理";
                    break;
                case "009012":
                    m.id = "/TakerSend/Index";
                    m.name = "派送员订单管理";
                    break;
                case "015002":
                    m.id = "/StatisticalReportSend/Index";
                    m.name = "派送费统计报表";
                    break;
                case "015001":
                    m.id = "/MerReport/Index";
                    m.name = "商户报表统计";
                    break;
                case "010001":
                    m.id = "/UserWallet/Index";
                    m.name = "用户钱包";
                    break;
                case "010002":
                    m.id = "/UserPlatformManage/Index";
                    m.name = "用户平台积分";
                    break;
                case "010003":
                    m.id = "/UserMerchantsManage/Index";
                    m.name = "用户商户积分";
                    break;
                case "010004":
                    m.id = "/User_District/Index";
                    m.name = "用户区域地址";
                    break;
                case "011001":
                    m.id = "/IntegralWater/Index";
                    m.name = "积分流水表";
                    break;
                case "011002":
                    m.id = "/FlowMon/Index";
                    m.name = "金额流水表";
                    break;
                case "011003":
                    m.id = "/FlowPay/Index";
                    m.name = "第三方支付流水表";
                    break;
                case "011004":
                    m.id = "/FlowOrganization/Index";
                    m.name = "企业帐号流水";
                    break;
                case "012001":
                    m.id = "/Taker/Index";
                    m.name = "派送员信息管理";
                    break;
                case "012002":
                    m.id = "/Send/Index";
                    m.name = "订单派送管理";
                    break;
                default:
                    break;

            }
            return m;
        }
        public class Zcd
        {
            public string id { get; set; }
            public string name { get; set; }
        }
        public class CDList
        {
            public string name { get; set; }
            public List<Zcd> zcdlist { get; set; }
        }
        //public ActionResult GetList()
        //{
        //    mml m = new mml();
        //    m.menu_id = "1";
        //    m.menu_name = "111";
        //    mml m2 = new mml();
        //    m2.menu_id = "2";
        //    m2.menu_name = "222";
        //    ll ll = new ll();
        //    ll.id = "0";
        //    ll.name = "000";
        //    List<mml> ls = new List<mml>();
        //    ls.Add(m); ls.Add(m2);
        //    ll.ls = ls;
        //    return Json(ll, JsonRequestBehavior.AllowGet);
        //}
        public ActionResult HomeContent()
        {
            return View();
        }
        public ActionResult getUser()
        {
            try
            {
                Model.View_User vv = (Model.View_User)Session[SessionStorage.UserLogin];//将Session保存的实体转成可直接使用的实体
                Model.View_OperUser dataresult = (Model.View_OperUser)Session[SessionStorage.OperUser];//将Session保存的实体转成可直接使用的实体
                if (vv != null)
                {
                    return Json(vv);
                }
                if (dataresult != null)
                {
                    return Json(dataresult);
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        /// <returns></returns>
        public ActionResult BackLogin()
        {
            try
            {
                Session[SessionStorage.UserLogin] = null;
                Session[SessionStorage.OperUser] = null;
                return Content("1");
            }
            catch (Exception ex) { return Content(ex.Message); }
        }
    }
}
